'''
Created on Mar 23, 2013

@author: Petko
'''
from django.views.generic.base import View
from sent.utils import createValidToken, CommonResult, ErrorIds
from sent.utils.result_printer import getPrinter
from sent.model.OpinionDBManager import OpinionDBManager

class ListOpinions(View):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        pass
    
    def get(self, *args, **kwargs):
        viewId = self.request.GET.get('view_id', None)
        order = self.request.GET.get('order', None)
        accessTokenStr = self.request.GET.get('access_token', None)
        
        cResult = CommonResult()
        
        try:
            maxContentLength = int(self.request.GET.get('max_content_length', 0))
            offset = int(self.request.GET.get('offset', 0))
            limit = int(self.request.GET.get('limit', 50))
        except ValueError:
            cResult.errorId = ErrorIds.BADREQUEST
            cResult.result = 'Bad format'
            return getPrinter().printResult(cResult)
        
        if viewId and order:
            dbMan = OpinionDBManager(True)
            try:
                if viewId == 'all_base_opinions':
                    if accessTokenStr:
                        accessToken, error = createValidToken(accessTokenStr, 'bopin_r')
                        if accessToken:
                            opinions, totalCount = dbMan.getAllBaseOpinions(order, offset, limit)
                            cResult.errorId = ErrorIds.SUCCESS
                        else:
                            cResult.errorId = ErrorIds.UNAUTHORIZED
                            cResult.result = error
                    else:
                        cResult.errorId = ErrorIds.BADREQUEST
                        cResult.result = "Missing access_token parameter"
                else:
                    view = dbMan.getViewById(viewId)
                    if view:
                        opinions, totalCount = dbMan.getViewOpinions(view, order, offset, limit)
                        cResult.errorId = ErrorIds.SUCCESS
                    else:
                        cResult.result = "View %s not found" % viewId
                        cResult.errorId = ErrorIds.NOT_FOUND
                
                if cResult.errorId == ErrorIds.SUCCESS:
                    if totalCount < 0:
                        cResult.errorId = ErrorIds.BADREQUEST
                        cResult.result = "Wrong Parameter"
                    else:
                        dbMan.fillOpinionTexts(opinions, maxContentLength)
                        
                        cResult.totalResults = totalCount
                        cResult.errorId = ErrorIds.SUCCESS
                        cResult.result = [opinion.toPrimitiveDict() for opinion in opinions]
            finally:
                dbMan.closeSession()
        else:
            cResult.errorId = ErrorIds.BADREQUEST
            cResult.result = "Missing parameter"
            
        return getPrinter().printResult(cResult)
    
    
